package com.hengpeng.itfin.tools;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.annotations.common.util.StringHelper;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class TemplateUtil
{
	private static Configuration cfg = new Configuration();

	/**
	 * <pre>
	 * 生成模板信息
	 * </pre>
	 * @param username 用户名
	 * @param activeUrl url
	 * @param siteName 站点名称
	 * @param domain 域名
	 * @param reserved 预留字段
	 * @param templateFile 模板名称
	 * @return
	 * @throws Exception
	 */
	public static String makeTemplateContent(String username,String activeUrl, String siteName,
			String domain, String reserved,String activeCode, String templateFile) throws Exception
	{
		cfg.setClassForTemplateLoading(TemplateUtil.class, "/template_ftl/");
		Map<String, String> root = new HashMap<String, String>();
		root.put("username", username);
		root.put("activeUrl", activeUrl);
		root.put("siteName", siteName);
		root.put("domain", domain);
		root.put("reserved", reserved);
		if (!StringHelper.isEmpty(activeCode)) {
			root.put("activeCode", activeCode);
		}
		Template t = cfg.getTemplate(templateFile, "utf-8");
		StringWriter writer = new StringWriter();
		t.process(root, writer);
		writer.flush();
		writer.close();
		return writer.toString();
	}

	/**
	 * <pre>
	 * 生成短信模板信息
	 * </pre>
	 * @param validCode 验证码
	 * @param siteName 站点名称
	 * @param reserved 预留字段
	 * @param templateFile 模板名称
	 * @return
	 * @throws Exception
	 */
	public static String makeSMSTemplateContent(String validCode, String siteName, String reserved, String templateFile)
			throws Exception
	{
		cfg.setClassForTemplateLoading(TemplateUtil.class, "/template_ftl/");
		Map<String, String> root = new HashMap<String, String>();
		root.put("validCode", validCode);
		root.put("siteName", siteName);
		root.put("reserved", reserved);
		Template t = cfg.getTemplate(templateFile, "utf-8");
		StringWriter writer = new StringWriter();
		t.process(root, writer);
		writer.flush();
		writer.close();
		return writer.toString();
	}


	public static void main(String args[]) throws Exception
	{
		// System.out.print(makeTemplateContent("zjt","www.baidu.com","registerActive.ftl"));
		// System.out.println(makeBonusAndPrintTicketTemplateContent("www.", "100", "100", "100", "100", "11", "100",
		// "01,02,03,04,05,06#07", "100", "100", "100", "100", "100", "100", "100", "100", "01,02,03,04,05,06#07",
		// "100", "是", "followPrintTicket.ftl"));
		// List<Plan> list = new ArrayList<Plan>();
		// for (int i = 0; i < 2; i++)
		// {
		// Plan plan = new Plan();
		// plan.setGameId(11);
		// plan.setSuccessCount(10);
		// plan.setTotalIssueCount(13);
		// plan.setBetMoney(2);
		// plan.setAutoLastIssueName("2012001");
		// plan.setIssueName("2012002");
		// plan.setStatusNote("2010-10-18 19：30（星期四）");
		// list.add(plan);
		//
		// }
		// System.out.println(makeNotEnoughMoneyTemplateContent("www.happycp.com", "100", "test", "开心彩票", "1.26", list,
		// "not_enough_money.ftl"));
	}

}
